/**
 * 
 */
package juf;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**Data source for accessing a field on an object and exposing
 * it using the Property interface
 * 
 * @author Dale Halliwell
 *
 */
public class FieldProperty implements Property {

	private Field field;
	private Object pojo;
	
	public FieldProperty(Field field, Object pojo){
		this.field = field;
		this.pojo = pojo;
	}
	
	/* (non-Javadoc)
	 * @see juf.Property#getName()
	 */
	@Override
	public String getName() {
		return field.getName();
	}

	/* (non-Javadoc)
	 * @see juf.Property#getType()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Class getType() {
		return field.getType();
	}

	/* (non-Javadoc)
	 * @see juf.Property#getValue()
	 */
	@Override
	public Object getValue() {
		try {
			return field.get(pojo);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see juf.Property#isReadOnly()
	 */
	@Override
	public boolean isReadOnly() {
		return false;
	}

	/* (non-Javadoc)
	 * @see juf.Property#setValue(java.lang.Object)
	 */
	@Override
	public void setValue(Object value) {
		try {
			field.set(pojo, value);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/* (non-Javadoc)
	 * @see juf.Property#getAnnotations()
	 */
	@Override
	public Annotation[] getAnnotations(){
		return field.getAnnotations();
	}

}
